package com.edu.exam.entity.curriculum.questionbank;

import com.edu.exam.enums.curriculum.questionbank.TransitionType;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;

/**
 * 试题状态转换历史实体类
 * 所属模块：B-02 题库管理模块
 * 对应数据库表：question_state_transitions
 *
 * <p>记录试题状态转换的详细历史信息，包括源状态、目标状态、转换原因、审批信息等。
 * 支持完整的状态变更追溯。</p>
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@Comment("试题状态转换历史表")
@SQLDelete(sql = "UPDATE question_state_transitions SET is_deleted = true, delete_time = CURRENT_TIMESTAMP WHERE id = ?")
@Where(clause = "is_deleted = false")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class QuestionStateTransition {

    /**
     * 状态转换记录唯一标识符
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    /**
     * 试题ID
     */
    @NotNull(message = "试题ID不能为空")
    @Column(name = "question_id", nullable = false)
    private Long questionId;

    /**
     * 源状态
     */
    @NotNull(message = "源状态不能为空")
    @Enumerated(EnumType.STRING)
    @Column(name = "from_state", nullable = false, length = 20)
    private QuestionStateType fromState;

    /**
     * 目标状态
     */
    @NotNull(message = "目标状态不能为空")
    @Enumerated(EnumType.STRING)
    @Column(name = "to_state", nullable = false, length = 20)
    private QuestionStateType toState;

    /**
     * 转换类型
     */
    @NotNull(message = "转换类型不能为空")
    @Enumerated(EnumType.STRING)
    @Column(name = "transition_type", nullable = false, length = 20)
    private TransitionType transitionType;

    /**
     * 转换原因
     */
    @Size(max = 500, message = "转换原因长度不能超过500个字符")
    @Column(name = "transition_reason", length = 500)
    private String transitionReason;

    /**
     * 操作人ID
     */
    @NotNull(message = "操作人ID不能为空")
    @Size(max = 50, message = "操作人ID长度不能超过50个字符")
    @Column(name = "operator_id", nullable = false, length = 50)
    private String operatorId;

    /**
     * 操作人姓名
     */
    @Size(max = 100, message = "操作人姓名长度不能超过100个字符")
    @Column(name = "operator_name", length = 100)
    private String operatorName;

    /**
     * 操作时间
     */
    @NotNull(message = "操作时间不能为空")
    @Column(name = "operation_time", nullable = false)
    private LocalDateTime operationTime;

    /**
     * 审批人ID
     */
    @Size(max = 50, message = "审批人ID长度不能超过50个字符")
    @Column(name = "approver_id", length = 50)
    private String approverId;

    /**
     * 审批人姓名
     */
    @Size(max = 100, message = "审批人姓名长度不能超过100个字符")
    @Column(name = "approver_name", length = 100)
    private String approverName;

    /**
     * 审批时间
     */
    @Column(name = "approval_time")
    private LocalDateTime approvalTime;

    /**
     * 审批备注
     */
    @Size(max = 500, message = "审批备注长度不能超过500个字符")
    @Column(name = "approval_remark", length = 500)
    private String approvalRemark;

    /**
     * 转换数据，JSON格式
     */
    @Column(name = "transition_data", columnDefinition = "TEXT")
    private String transitionData;

    /**
     * 记录创建时间
     */
    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;

    /**
     * 创建人用户ID
     */
    @Size(max = 50, message = "创建人长度不能超过50个字符")
    @Column(name = "created_by", nullable = false, length = 50)
    private String createdBy;

    /**
     * 乐观锁版本号
     */
    @Version
    @Column(name = "version", nullable = false)
    @Builder.Default
    private Integer version = 1;

    /**
     * 软删除标记
     */
    @Column(name = "is_deleted", nullable = false)
    @Builder.Default
    private Boolean isDeleted = false;

    /**
     * 删除时间
     */
    @Column(name = "delete_time")
    private LocalDateTime deleteTime;

    /**
     * 试题状态类型枚举
     */
    public enum QuestionStateType {
        /** 草稿 */
        DRAFT,
        /** 已发布 */
        PUBLISHED,
        /** 已归档 */
        ARCHIVED,
        /** 审核中 */
        UNDER_REVIEW,
        /** 已拒绝 */
        REJECTED
    }
}